package com.gdufe.seckill.service.impl;

import com.alibaba.fastjson.TypeReference;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.gdufe.common.entity.Product;
import com.gdufe.common.feign.ProductFeign;
import com.gdufe.common.utils.Query;

import com.gdufe.seckill.dto.SeckillRelationDto;
import com.gdufe.seckill.entity.SessionEntity;
import com.gdufe.seckill.service.SessionAdminService;
import com.gdufe.seckill.vo.SeckillProDetailsVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.BoundHashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gdufe.common.utils.PageUtils;

import com.gdufe.seckill.dao.RelationDao;
import com.gdufe.seckill.entity.RelationEntity;
import com.gdufe.seckill.service.RelationAdminService;

@Service("RelationAdminService")
public class RelationAdminServiceImpl extends ServiceImpl<RelationDao, RelationEntity> implements RelationAdminService {

    @Autowired
    private ProductFeign productFeign;
    @Autowired
    private SessionAdminService sessionAdminService;
    @Autowired
    private RedisTemplate redisTemplate;
    public  static final String SEC_KILL_GOODS_KEY="seckillgoods";

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        int pageNum = Integer.parseInt((String) params.get("page"));
        //获取用户输入的查询条件
        String key = (String) params.get("key");
        String sessionId = (String) params.get("sessionId");
        QueryWrapper<RelationEntity> wrapper = new QueryWrapper<>();
        //如果key非空则根据秒杀id以及商品id对商品进行筛选
        if(!key.isEmpty()){
            wrapper.eq("id",key).or().eq("product_id",key);
        }
        //如果场次id非空则根据场次id对商品进行筛选
        if(!sessionId.isEmpty()){
            wrapper.eq("session_id",sessionId);
        }
        IPage<RelationEntity> page = this.page(
                new Query<RelationEntity>().getPage(params),
                wrapper
        );

        params.put("page", pageNum);
        return new PageUtils(page);
    }

    @Override
    public SeckillProDetailsVo sessionDetails(Integer id) {
        //获取秒杀商品的详细信息（增加商品名称、场次时间段）
        SeckillProDetailsVo vo =new SeckillProDetailsVo();
        RelationEntity relation = this.getById(id);
        BeanUtils.copyProperties(relation,vo);
        SessionEntity session =  sessionAdminService.getById(relation.getSessionId());
        vo.setEndTime(session.getEndTime());
        vo.setStartTime(session.getStartTime());
        Product product = productFeign.adminSelectById(relation.getProductId()).getData(new TypeReference<Product>(){});
        vo.setProductName(product.getName());
        return vo;
    }

    @Override
    public void updateStstus(SeckillRelationDto dto) {
        RelationEntity relation = this.getById(dto.getId());
        relation.setSeckillStatus(dto.getSeckillStatus());
        super.updateById(relation);
        //获取操作redis hash类型的操作类
        BoundHashOperations<String,Object ,Object> hashOperations = redisTemplate.boundHashOps(SEC_KILL_GOODS_KEY);

        if(dto.getSeckillStatus()==0){
            hashOperations.delete(dto.getId().toString());
        }else{
            hashOperations.put(relation.getId().toString(),relation.getSeckillCount().toString());
        }
    }


}